export default [
  {
    path: '/',
    component: () => import('@/views/Home'),
    meta: {
      title: '首页',
      showFooter: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login'),
    meta: {
      title: '登录',
      showFooter: false
    }
  },
  {
    path: '/register',
    component: () => import('@/views/Register'),
    meta: {
      title: '注册',
      showFooter: false
    }
  },
  {
    name: 'search',
    path: '/search/:keyword?',// params参数可传可不传
    component: () => import('@/views/Search'),
    meta: {
      title: '搜索',
      showFooter: true
    }
  },
  {
    name: 'gooddetail',
    path: '/gooddetail/:skuId',
    component: () => import('@/views/Detail'),
    meta: {
      title: '详情',
      showFooter: true
    }
  },
  {
    name: 'addcartsuccess',
    path: '/addcartsuccess',
    component: () => import('@/views/AddCartSuccess'),
    meta: {
      title: '加入购物车',
      showFooter: true
    },
    beforeEnter(to, from, next) {
      const skuNum = to.query.skuNum
      const skuInfo = JSON.parse(window.sessionStorage.getItem('SKUINFO'))
      console.log(from,skuNum,skuInfo)
      if (skuNum && skuInfo) {
        next()
      } else { // 在组件对象创建前强制跳转到首页
        next('/')
      }
    }
  },
  {
    name: 'shopcart',
    path: '/shopcart',
    component: () => import('@/views/ShopCart'),
    meta: {
      title: '购物车',
      showFooter: true
    }
  },
  {
    name: 'trade',
    path: '/trade',
    component: () => import('@/views/Trade'),
    meta: {
      title: '订单详情',
      showFooter: true
    },
    // 只能从购物车界面，才能跳转到交易页面
    beforeEnter(to, from, next){
      if(from.path === '/shopcart'){
        next()
      }else{
        next('/shopcart')
      }
    }
  },
  {
    name: 'pay',
    path: '/pay',
    component: () => import('@/views/Pay'),
    meta: {
      title: '支付页面',
      showFooter: true
    },
    // 将query参数映射成props传递给路由组件
    props: route => ({orderId: route.query.orderId}),
    /* 只能从交易界面, 才能跳转到支付界面 */
    beforeEnter(to, from, next){
      if(from.path === '/trade'){
        next()
      }else{
        next('/trade')
      }
    }
  },
  {
    name: 'paysuccess',
    path: '/paysuccess',
    component: () => import('@/views/PaySuccess'),
    meta: {
      title: '支付成功页面',
      showFooter: true
    },
    /* 只能从交易界面, 才能跳转到支付界面 */
    beforeEnter(to, from, next){
      if(from.path === '/pay'){
        next()
      }else{
        next('/pay')
      }
    }
  },
  {
    name: 'myorder',
    path: '/myorder',
    component: () => import('@/views/MyOrder'),
    meta: {
      title: '我的订单',
      showFooter: true
    }
  },
]